package com.yellowpage.white.struts;

import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.UserService;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.Logger;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.util.StringTools;

public class BaseAction extends ActionSupport implements Preparable,SessionAware{
	private static final long serialVersionUID = 3432154234535234534l;

	protected Log log = LogFactory.getLog(this.getClass());
    
    protected Long userId;
    protected User loginUser;
    protected String loginName;
            
    protected Date nowDate;
    
    protected PageRender pageRender;
    

    protected Logger logger;

    @SuppressWarnings("unchecked")
    protected Map session;
    
    private String validatorResult;

	

	public void prepare() {
		
		 System.out.println("****** BaseAction invoke.... ");
		
		SessionMap session = (SessionMap)ServletActionContext.getContext().get(ServletActionContext.SESSION);
		
		HttpServletRequest request =   ServletActionContext.getRequest();
		
		
//		try{
//		    request.setCharacterEncoding("UTF-8");
//		}catch(Exception e){
//			e.printStackTrace();
//		}
				
		loginName= request.getUserPrincipal().getName();
		
		System.out.println("****** BaseAction  loginName = " + loginName);
		
		loginUser = (User)session.get(Constants.get("USER_OBJECT"));
		
		WebApplicationContext wactx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
     
		if(loginUser==null){
			UserService userService = (UserService)wactx.getBean("userService");
			loginUser = userService.getUserByName(loginName);       
			
			session.put(Constants.get("USER_OBJECT"),loginUser);
			
			userId= loginUser.getUserId();
		}else{
            userId = loginUser.getUserId();
		}
		
		ActionContext ctx = ServletActionContext.getContext();
		logger = (Logger) wactx.getBean("logger");
		
		
		
        nowDate = StringTools.string2date(StringTools.date2string(new Date()),"yyyy-MM-dd HH:mm:ss");

	}

	
	protected void log(String module, String operationType,String content) {
		ActionContext ctx = ServletActionContext.getContext();

		HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
		String clientIp = request.getRemoteHost();
		
		logger.log(module, operationType,content, loginName, clientIp);
	}
	
	public void initPageRender(int totalNum){
		HttpServletRequest request = ServletActionContext.getRequest();
		pageRender = new PageRender(request,totalNum);		
	}	
	
	public void initPageRender(){
		HttpServletRequest request = ServletActionContext.getRequest();
		pageRender = new PageRender(request);		
	}
	
	public void setPageRenderToRequest(){
		HttpServletRequest request = ServletActionContext.getRequest();		
		pageRender.doPageBreak();		
		request.setAttribute("pageRender", pageRender);
	}
	
	
	public void setPageRenderToRequestNoCount(){
		HttpServletRequest request = ServletActionContext.getRequest();		
		pageRender.doPageBreakNoCount();		
		request.setAttribute("pageRender", pageRender);
	}
	    
	/**
	 * @param session the session to set
	 */
	@SuppressWarnings("unchecked")
	public void setSession(Map session) {
		this.session = session;
	}

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }
    
    public String getValidatorResult() {
		return validatorResult;
	}


	public void setValidatorResult(String validatorResult) {
		this.validatorResult = validatorResult;
	}
    
  


}
